home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf / Programming / Random / random.asm < prev    next >
Assembly Source File  |  1992-03-30  |  3KB  |  133 lines

  1.     ;------------------------------------------------------------------
  2.     ; Code typed by Peter Thompson on 11 Feb. 92, although written
  3.     ; several days earlier. This code is "wareware"; if you use it in
  4.     ; any of your programs for which you want money, send me a copy at:
  5.     ; 33 Pleasant Street/Pascoe Vale 3044/Australia
  6.     ;------------------------------------------------------------------
  7.     ; $MUSIC = "The Black Sorrows/Hold On To Me"
  8.     ;------------------------------------------------------------------
  9.     ; Algorithm adapted from "Algorithms in C", Robert Sedgewick,
  10.     ; pub. Addison-Wesley 1990 (he got it from Knuth, vol. 2?)
  11.     ;------------------------------------------------------------------
  12.  
  13. C_GLUE    SET 1
  14. rs_Index        EQU    0
  15. rs_State        EQU    4
  16. RandState_SIZEOF    EQU    4+64*4
  17.  
  18.     IFD    C_GLUE
  19.  
  20. ;') void rand32init(RandState *d0, ULONG Seed d0)
  21.     PUBLIC    _rand32init
  22. ;') ULONG d0 = rnda32(RandState *d0)
  23.     PUBLIC    _rnda32
  24. ;') ULONG d0 = rndx32(RandState *d0)
  25.     PUBLIC    _rndx32
  26.  
  27.     ENDC
  28.     IFND    C_GLUE
  29.  
  30.     PUBLIC    rand32init
  31.     PUBLIC    rnda32
  32.     PUBLIC    rndx32
  33.     PUBLIC    RandState_SIZEOF
  34.  
  35.     ENDC
  36.  
  37. ;ULONG d0 = rndm32(ULONG d0)
  38. ;alters: d0
  39.     IFD    C_GLUE
  40. _rndm32:
  41.     move.l    4(SP),d0
  42.     ENDC
  43. rndm32:
  44.     movem.l    d1-d3,-(SP)    ;d0 d1 d2 d3
  45.     move.l    #31415821,d1    ;ab cd -- --
  46.     move.l    d0,d2        ;ab cd ab --
  47.     mulu    d1,d0        ;BD cd ab --
  48.     move.l    d2,d3        ;BD cd ab ab
  49.     swap    d3        ;BD cd ab ba
  50.     mulu    d1,d3        ;BD cd ab AD
  51.     swap    d1        ;BD dc ab AD
  52.     mulu    d1,d2        ;BD dc BC AD
  53.     add.l    d2,d3        
  54.     swap    d3
  55.     clr.w    d3
  56.     add.l    d3,d0
  57.     addq.l    #1,d0
  58.     movem.l    (SP)+,d1-d3
  59.     rts
  60.  
  61. ;void rand32init(RandState *d0, ULONG d1 Seed)
  62. ;alters: d0,d1,a0,a1
  63.     IFD    C_GLUE
  64. _rand32init:
  65.     move.l    4(SP),d0
  66.     move.l    8(SP),d1
  67.     ENDC
  68. rand32init:
  69.     move.l    d0,a0
  70.     move.l    d0,a1
  71.     move.l    d1,d0
  72.     moveq    #64,d1
  73. riloop:    move.l    d0,(a0)+
  74.     bsr    rndm32
  75.     dbra    d1,riloop
  76.     moveq    #0,d0
  77.     move.l    d0,rs_Index(a1)
  78.     rts
  79.  
  80. ;ULONG d0 = rndx32(RandState *d0)
  81. ;alters: d0,d1,a0
  82.     IFD    C_GLUE
  83. _rndx32:
  84.     move.l    4(SP),d0
  85.     ENDC
  86. rndx32:    move.l    d2,-(SP)
  87.     tst.l    d0
  88.     beq.S    rndx32ZZ
  89.     move.l    d0,a0
  90.     move.l    rs_Index(a0),d1        ; index = (index+1) mod 64
  91.     addq.b    #4,d1
  92.     move.l    d1,rs_Index(a0)
  93.     moveq    #4*(64-55),d0    ; state[index] =
  94.     add.b    d1,d0        ;   state[(index+(64-55)) mod 64] xor
  95.     move.l    rs_State(a0,d0.w),d2
  96.     move.b    #4*(64-31),d0    ;   state[(index+(64-31)) mod 64];
  97.     add.b    d1,d0
  98.     move.l    rs_State(a0,d0.w),d0
  99.     eor.l    d2,d0
  100.     move.l    d0,rs_State(a0,d1.w)
  101. rndx32ZZ:
  102.     lsr.l    #1,d0
  103.     move.l    (SP)+,d2
  104.     rts
  105.  
  106. ;ULONG d0 = rnda32(RandState *d0)
  107. ;alters: d0,d1,a0
  108.     IFD    C_GLUE
  109. _rnda32:
  110.     move.l    4(SP),d0
  111.     ENDC
  112. rnda32:    move.l    d2,-(SP)
  113.     tst.l    d0
  114.     beq.S    rnda32ZZ
  115.     move.l    d0,a0
  116.     move.l    rs_Index(a0),d1        ; index = (index+1) mod 64
  117.     addq.b    #4,d1
  118.     move.l    d1,rs_Index(a0)
  119.     moveq    #4*(64-55),d0    ; state[index] =
  120.     add.b    d1,d0        ;   state[(index+(64-55)) mod 64] +
  121.     move.l    rs_State(a0,d0.w),d2
  122.     move.b    #4*(64-31),d0    ;   state[(index+(64-31)) mod 64];
  123.     add.b    d1,d0
  124.     move.l    rs_State(a0,d0.w),d0
  125.     add.l    d2,d0
  126.     move.l    d0,rs_State(a0,d1.w)
  127. rnda32ZZ:
  128.     lsr.l    #1,d0
  129.     move.l    (SP)+,d2
  130.     rts
  131.  
  132.     END
  133.